<?php

/*
 *
 *
 * This software has been made FREELY available by author for interest of the developers. 
 *      I hope this will help you in coding faster and robust php application
 *            You can modify and use it as you it suits you.
 *              Your comments, suggestions and corrections 
 *                          will be highly 
 *                           APPRECIATED. 
 *                                      Thanks, 
 *                                      Author
 * @author :   Roshan Budhathoki (contact@roshanbudhathoki.com);
 * @URL    :   <http://www.roshanbudhathoki.com>
 *
 */
class Breadcrumb {
	
	public $_items;
	public $_description;
	private static $instance;
	
	public static function newInstance() {
		if (! self::$instance instanceof self) {
			self::$instance = new self ();
		}
		return self::$instance;
	}
	
	public function __construct() {
		$this->_items = array ();
	}
	
	public function add($title, $link) {
		if (! is_array ( $this->_items )) {
			$this->_items = array ();
		}
		$this->_items [] = array ('title' => $title, 'link' => $link );
	}
	
	public function getPageTitle() {
		$items = $this->_items;
		$title = array ();
		//print_r($items);
		if ($items) {
			foreach ( $items as $key => $value ) {
				$title [] = $value ['title'];
			}
		} else {
			$title [] = preference::newInstance ()->getPreference ( 'site', 'site_title' );
		}
		unset ( $items );
		return implode ( '|', $title ) . '|' . $this->getSiteTitle ();
		unset ( $title );
	}
	
	public function getSiteTitle() {
		return preference::newInstance ()->getPreference ( 'site', 'site_title' );
	}
	
	public function addDescription($desc) {
		$this->_description = $desc;
	}
	
	public function render() {
		$items = array_reverse ( $this->_items );
		$count = count ( $items );
		if (is_array ( $items )) {
			$urls = array ();
			$urls [] = '<div id="breadcrumb">';
			$urls [] = '<ul><li><a href="' . SITE_URL . '"><span class="you_are_here">::You are here&raquo;</span>&nbsp;&nbsp;Home&nbsp;&nbsp;&raquo;</a></li>';
			foreach ( $items as $key => $value ) {
				if ($key == ($count - 1)) {
					$urls [] = '<li><a href="' . $value ['link'] . '">' . $value ['title'] . '&nbsp;&nbsp;</a></li>';
				} else {
					$urls [] = '<li><a href="' . $value ['link'] . '">' . $value ['title'] . '&nbsp;&nbsp;&raquo;</a></li>';
				}
			}
			$urls [] = '</ul>';
			$urls [] = '</div>';
		}
		return implode ( "", $urls );
		unset ( $items );
		unset ( $urls );
	}
	
	public function getPageDescription() {
		return $this->_description;
	}

}
